<?php

namespace App\Console\Commands;

use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;

class ClearReportsCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'report:clear';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = "Clear the error reports";

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $reportsDir = realpath(ROOT_PATH.'/storage/reports');
        $skipList = [
            '.',
            '..',
            '.gitignore'
        ];
        $handle = opendir($reportsDir);
        if ($handle) {
            while (($fileName = readdir($handle)) !== false) {
                if (in_array($fileName, $skipList)) {
                    continue;
                } else {
                    if (unlink($reportsDir.'/'.$fileName)) {
                        $this->line('Remove "'.$fileName.'" successfully.');
                    } else {
                        $this->line('Remove "'.$fileName.'" failed.');
                    }
                }
            }
        } else {
            $this->line('Open reports folder failed.');
        }
        $this->info('Clear reports over.');
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [];
    }
}
